package foo.bar.forum.services;

import java.net.URISyntaxException;
import java.net.URI;
import java.util.LinkedHashSet;
import java.util.Set;
import java.io.File;
import java.io.FileFilter;

/**
 * @author tmjee
 */
public class ThemeService {

    ThemeService(){}

    public Set<String> getAllWikiThemeNames() throws URISyntaxException {
        // TODO: io access everytime? can we cache this?
        URI wikiThemeUri= getClass().getResource("/theme").toURI();
        File wikiThemeDir = new File(wikiThemeUri);

        File[] wikiThemeFiles = wikiThemeDir.listFiles(new FileFilter(){
            public boolean accept(File pathname) {
                if (pathname.getName().toLowerCase().endsWith(".properties")) {
                    return true;
                }
                return false;
            }
        });

        Set<String> wikiThemeNames = new LinkedHashSet<String>();
        for (File wikiThemeFile : wikiThemeFiles) {
            String filename = wikiThemeFile.getName();
            int index = filename.indexOf(".properties");
            if (index > 0) {
                wikiThemeNames.add(wikiThemeFile.getName().substring(0, index));
            }
        }

        return wikiThemeNames;
    }

}
